
/**
  * @param {array} whileList 不需要重置的白名单
  * @param {object} obj 需要重置的对象
  * @param {Function} db 回调方法
  * @description 重置白名单之外的所有内容
  */
export const reset = (whileList = [], obj, db) => {
  Object.keys(obj).forEach(key => {
    if (whileList.indexOf(key) === -1) {
      if (!obj[key] || typeof obj[key] === "string" || typeof obj[key] === "number") {
        obj[key] = "";
      } else if (Array.isArray(obj[key])) {
        obj[key] = [];
      } else if (typeof obj[key] === "boolean") {
        obj[key] = false;
      } else {
        obj[key] = {};
      }
    }
  });

  // 回调方法
  db && db();
};

/**
  * @param {object} form 查询条件
  * @param {string} field
  * @param {that} this
  * @return 将分页条件设置进查询条件中
  */
export const getPagination = (form, field, that) => {
  const { pageSize, pageNum } = that[field].pagination;
  const _data = {
    pageSize, pageNum,
    ...form
  };
  return _data;
};

/**
  * @param {object} table 设置的表格字段
  * @param {that} this
  * @param {Object} pagination 新的分页数据
  * @param {Function} db 回调方法
  * @description 修改制定表格中的分页数据
  */
export const changePagination = (table, that, pagination, db) => {
  const { pageSize, currentPage } = pagination;
  that[table].pagination = Object.assign(that[table].pagination, {
    pageSize,
    currentPage
  });
  db && db();
};

/**
  * @param {object} table 设置的表格字段
  * @param {that} this
  * @param {Object} multipleSelection 新的选中数据
  * @param {Function} db 回调方法
  * @description 获取选中数据
  */
export const selectChange = (table, that, multipleSelection, db) => {
  if (!that[table].multipleSelection) {
    throw new Error(`请在${table}字段中添加 multipleSelection 属性用于接收表格选中数据`);
  }
  that[table].multipleSelection = multipleSelection;
  db && db();
};

/**
  * @param {string} field 需要写入数据的字段
  * @param {that} this
  * @param {Function} api 获取数据的接口
  * @param {object} form 获取数据接口参数
  * @param {boolean} isPagination 接口获取的数据是否有分页
  * @param {db} 回调方法
  * @description 获取表格数据
  */
export const getTableData = async(field, that, api, form, isPagination, db) => {
  if (typeof that[field].loading === "undefined") {
    throw new Error("请将loading属性加上避免接口获取等待时间太长");
  }

  try {
    that[field].loading = true;
    const data = await api(form);
    that[field].loading = false;
    // 无分页
    if (!isPagination) {
      // 并且没有回调方法
      if (!db) {
        // 直接获取数据
        that[field].data = data;
        // 数据总数  -------- 可能在回调方法中设置数据条数 所以不放在 if 语句外面
        that[field].pagination.total = data.length;
      } else {
        try {
          // 数据总数
          that[field].pagination.total = data.length;
          // 使用回调方法设置数据
          db(data);
        } catch (error) {
          console.log("无分页回调方法错误");
          that[field].data = [];
          that[field].pagination.total = 0;
        }
      }
    } else if (isPagination) {
      // 有分页
      const { records, total } = data || {};
      if (!db) {
        // 直接获取数据
        that[field].data = records;
        // 数据总数  -------- 可能在回调方法中设置数据条数 所以不放在 if 语句外面
        that[field].pagination.total = total || 0;
      } else {
        try {
          // 数据总数
          that[field].pagination.total = total || 0;
          db(records || data);
        } catch (error) {
          console.log("有分页回调方法错误");
          that[field].data = [];
          that[field].pagination.total = 0;
        }
      }
    }
  } catch (error) {
    console.log("api调用报错");
    that[field].data = [];
    that[field].pagination.total = 0;
    that[field].loading = false;
  }
};

/**
  * @param {number} number 需要展开的数量
  * @param {object} originObj 需要修改的对象
  * @param {flag} 展开收缩的标识
  * @param {that} this
  * @description 设置表格高度
  */
export const getTableHeight = (form, table, that, defaultHeight, otherHeight) => {
  if (!defaultHeight || !that) {
    throw new Error("参数错误");
  }
  that.$nextTick(() => {
    if (form) {
      const formHeight = that.$refs[form]?.offsetHeight;
      const tableHeight = (defaultHeight + formHeight) - (otherHeight || 0);
      that[table].height = `calc( 100vh - ${tableHeight}px )`;
      return;
    }
    that[table].height = `calc( 100vh - ${defaultHeight}px )`;
  });
};

/**
  * @param {object} form 查询的参数
  * @param {Array}  time elementUI 的时间格式
  * @param {Array} timeField 需要修改的时间格式
  * @description 时间格式转换
  */
export const transitionDateFormat = (form, oldTimeField, timeField = ["staDate", "endDate"]) => {
  for (let i = 0; i < timeField.length; i++) {
    form[timeField[i]] = form[oldTimeField][i] || "";
  }
  return form;
};
